import {createRouter, createWebHistory} from 'vue-router'
import LoginView from "@/views/LoginView.vue";
import HomeView from "@/views/HomeView.vue";
import IndexView from '@/views/IndexView.vue'
import ProductListView from '@/views/ProductListView.vue'
import RegView from "@/views/RegView.vue";
import {useTokenStore} from "@/stores/token.js";
import ProductView from "@/views/ProductView.vue";
import CartView from "@/views/CartView.vue";
import CreateOrderView from "@/views/CreateOrderView.vue";
import CenterView from "@/views/CenterView.vue";
import AddrListView from "@/views/center/AddrListView.vue";
import CollectListView from "@/views/center/CollectListView.vue";
import InfoView from "@/views/center/InfoView.vue";
import OrderListView from "@/views/center/OrderListView.vue"
import PasswordView from "@/views/center/PasswordView.vue";
import RechargeView from "@/views/center/RechargeView.vue";
import PayView from "@/views/PayView.vue";

const router = createRouter({
	history: createWebHistory(import.meta.env.BASE_URL),
	routes: [
		{
			path: '/',
			name: 'home',
			component: HomeView,
			redirect: '/index',
			children: [
				{
					path: '/login',
					name: 'login',
					component: LoginView
				}, {
					path: '/index',
					name: 'index',
					component: IndexView
				}, {
					path: '/productList/:categoryId?',
					name: 'product_list',
					props: true,
					component: ProductListView
				}, {
					path: '/reg',
					name: 'reg',
					component: RegView
				}, {
					path: '/product',
					name: 'product',
					component: ProductView
				}, {
					path: '/cart',
					name: 'cart',
					component: CartView
				}, {
					path: '/createOrder',
					name: 'createOrder',
					component: CreateOrderView
				},  {
					path: '/pay',
					name: 'pay',
					component: PayView
				}, {
					path: '/center',
					name: 'center',
					component: CenterView,
					redirect: '/info',
					children: [
						{
							path: '/addrList',
							name: 'addrList',
							component: AddrListView
						}, {
							path: '/collectList',
							name: 'collectList',
							component: CollectListView
						}, {
							path: '/info',
							name: 'info',
							component: InfoView
						}, {
							path: '/orderList',
							name: 'orderList',
							component: OrderListView
						}, {
							path: '/password',
							name: 'password',
							component: PasswordView
						}, {
							path: '/recharge',
							name: 'recharge',
							component: RechargeView
						}
					]
				}
			]
		}
	]
})

/*
* 导航守卫
* 1. 配置导航守卫目的?
* 	判断页面跳转是不是合法
* 		合法 - 跳转
* 		不合法 - 不跳转/跳转到特定的页面
* 2. 应用?
* 	判断是否已经登录
* 		登录 - 允许跳转
* 		未登录 - 跳转到登录页
* */
router.beforeEach((to, from) => {
	//to - 跳转到的位置
	//from - 原位置
	//TODO 将来还会对其他页面放行（例如：前台首页，搜索页，详情页，用户登录/注册页）
	if(to.path == '/' ||
		to.path == "/login" ||
		to.path == "/index" ||
		to.path == "/reg" ||
		to.path == "/goods" ||
		to.path == "/product" ||
		to.path.startsWith("/productList")) {
		return true;
	} else {
		//判断是否有token 有 - 放行 没有 - 跳转到登录页
		const tokenStore = useTokenStore();
		if(tokenStore.tokenStr) {
			return true;
		} else {
			return "/login";
		}
	}
})

export default router
